package fractal.fractal;

import jinyilw.common.math.Complex;

public class NewtonFractal extends Fractal
{
	private static final double[] ONE = {1.0F, 0.0F};
	private static final double[] THREE = {3.0F, 0.0F};

	@Override
	public int calculateN(double x, double y, int maxN, double R)
	{
		double[] c1, c2, c3, c4;
		c1 = new double[2];
		c1[0] = x;
		c1[1] = y;
		int N;
		for (N = 0; N < maxN; N++)
		{
			c3 = Complex.sqr(c1);
			c4 = Complex.mul(c3, c1);
			c2 = c1;
			c1 = Complex.sub(c1,
					Complex.div(Complex.sub(c4, ONE), Complex.mul(THREE, c3)));
			if (Complex.equal(c1, c2))
				return N;
		}
		return N;
	}

	@Override
	public void initialise()
	{
		maxN = 300;
		xmin = -1.5;
		ymin = -1.35;
		dxy = 3.0 / width;
		R = 8;
		setAngle(0);
		setSff(1);
	}

}
